package com.jml.hystrix.controller;


import com.alibaba.fastjson.JSONObject;
import com.jml.hystrix.MemberHystrixCommand;
import com.jml.hystrix.OrderHystrixCommand;
import com.jml.hystrix.service.MemberService;
import com.jml.hystrix.utils.HttpClientUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class OrderController {

	@Autowired
	MemberService memberService;

	@RequestMapping("/")
	public String order() throws InterruptedException {
		String a = getTh();
		Thread.sleep(3000);
		System.out.println(a);
		 a = getTh();
		return a;
	}

	private String getTh() {

		for (int i = 0; i < 100; i++) {
			new Thread(new Runnable() {
				@Override
				public void run() {
					JSONObject result = HttpClientUtils.httpGet("http://127.0.0.1:8081/member/memberIndex");
				}
			});
		}

		return "111";
	}

	//解决服务雪崩方式
	@RequestMapping("/orderHystrix001")
	public Object orderHystrix(){
		return new OrderHystrixCommand(memberService).execute();
	}

	@RequestMapping("/orderHystrix002")
	public Object orderHystrix001(){
		return new OrderHystrixCommand(memberService).execute();
	}

	@RequestMapping("/memberHystrix001")
	public Object memberHystrix001(){
		return new MemberHystrixCommand(memberService).execute();
	}

	@RequestMapping("/memberHystrix002")
	public Object memberHystrix002(){
		return new MemberHystrixCommand(memberService).execute();
	}

}
